package cn.JHai.rbac.controller;
import cn.JHai.rbac.RequiredPermission;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.JHai.rbac.domain.Customer;
import cn.JHai.rbac.query.*;
import cn.JHai.rbac.service.CustomerService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import java.util.List;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    /**
     * 显示customer，运用了分页
     * @param queryObject
     * @param model
     * @return
     */



    @RequestMapping("/list")
    public String list(QueryObject queryObject , QueryCustomer queryCustomer, Model model){

        PageResult result = customerService.query(queryObject,queryCustomer);
        List<QueryNameAndId> list = customerService.selectSeller();
        model.addAttribute("lists",list);
        model.addAttribute("qo",queryCustomer);
        model.addAttribute("result",result);
        return "/customer/list";
    }




    @RequestMapping("/potentialList")
    @RequiredPermission({"潜在客户列表","customer:potentialList"})
    public String potentialList(QueryObject queryObject , QueryCustomer queryCustomer, Model model){
        queryCustomer.setStatus("0");
        PageResult result = customerService.query(queryObject,queryCustomer);
        List<QueryNameAndId> employees = customerService.selectSeller();
        List<QueryTittleAndId> jobs = customerService.selectJobs();
        List<QueryTittleAndId> sources = customerService.selectsources();
        List<QueryTittleAndId> ccts = customerService.selectContacts();
        model.addAttribute("employees",employees);
        model.addAttribute("sources",sources);
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("qo",queryCustomer);
        model.addAttribute("result",result);

        return "/customer/potentialList";
    }
    @RequestMapping("/poolList")
    @RequiredPermission({"客户池列表","customer:poolList"})
    public String pool_list(QueryObject queryObject , QueryCustomer queryCustomer, Model model){
        queryCustomer.setStatus("4");
        PageResult result = customerService.query(queryObject,queryCustomer);
        List<QueryNameAndId> employees = customerService.selectSeller();
        List<QueryTittleAndId> jobs = customerService.selectJobs();
        List<QueryTittleAndId> sources = customerService.selectsources();
        List<QueryTittleAndId> ccts = customerService.selectContacts();
        model.addAttribute("employees",employees);
        model.addAttribute("sources",sources);
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("qo",queryCustomer);
        model.addAttribute("sellers",employees);
        model.addAttribute("result",result);
        return "/customer/pool_list";
    }

    @RequestMapping("/failList")
    @RequiredPermission({"失败客户列表","customer:poolList"})
    public String fail_list(QueryObject queryObject , QueryCustomer queryCustomer, Model model){
        queryCustomer.setStatus("2");
        PageResult result = customerService.query(queryObject,queryCustomer);
        List<QueryNameAndId> employees = customerService.selectSeller();
        List<QueryTittleAndId> jobs = customerService.selectJobs();
        List<QueryTittleAndId> sources = customerService.selectsources();
        List<QueryTittleAndId> ccts = customerService.selectContacts();
        model.addAttribute("employees",employees);
        model.addAttribute("sellers",employees);
        model.addAttribute("sources",sources);
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("qo",queryCustomer);
        model.addAttribute("result",result);
        return "/customer/fail_list";
    }


    @RequestMapping("/formalList")
    @RequiredPermission({"正式客户列表","customer:formalList"})
    public String formal_list(QueryObject queryObject , QueryCustomer queryCustomer, Model model){
        queryCustomer.setStatus("1");
        PageResult result = customerService.query(queryObject,queryCustomer);
        List<QueryNameAndId> employees = customerService.selectSeller();
        List<QueryTittleAndId> jobs = customerService.selectJobs();
        List<QueryTittleAndId> sources = customerService.selectsources();
        List<QueryTittleAndId> ccts = customerService.selectContacts();
        model.addAttribute("employees",employees);
        model.addAttribute("sellers",employees);
        model.addAttribute("sources",sources);
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("qo",queryCustomer);
        model.addAttribute("result",result);
        return "/customer/formal_list";
    }

    @RequestMapping("/loseList")
    @RequiredPermission({"流失客户列表","customer:loseList"})
    public String LostList(QueryObject queryObject , QueryCustomer queryCustomer, Model model){
        queryCustomer.setStatus("3");
        PageResult result = customerService.query(queryObject,queryCustomer);
        List<QueryNameAndId> employees = customerService.selectSeller();
        List<QueryTittleAndId> jobs = customerService.selectJobs();
        List<QueryTittleAndId> sources = customerService.selectsources();
        List<QueryTittleAndId> ccts = customerService.selectContacts();
        model.addAttribute("employees",employees);
        model.addAttribute("sellers",employees);
        model.addAttribute("sources",sources);
        model.addAttribute("ccts",ccts);
        model.addAttribute("jobs",jobs);
        model.addAttribute("qo",queryCustomer);
        model.addAttribute("result",result);
        return "/customer/lose_list";
    }

    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    //@RequiredPermission({"删除部门","customer:delete"})
    public String list(  Model model,Long id){
        customerService.Delete(id);
        return "redirect:/customer/list";
    }


    @RequestMapping("input")

    public String input(Model model,Long id){
        if(id!=null){
            model.addAttribute("entity",customerService.get(id));
        }
        return "customer/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"客户添加/更新","customer:saveOrUpdate"})
    @ResponseBody
    //@RequiredPermission({"修改部门","customer:saveOrUpdate"})
    public JSONObject saveOrUpdate(Customer customer){
        System.out.println(customer);
        customerService.saveOrUpdata(customer);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",true);
        return jsonObject;
    }

    @RequestMapping("/updateStatus")
    @RequiredPermission({"客户状态修改","customer:updateStatus"})
    @ResponseBody
    public JSONObject updateStatus(Long customerId,Long status){

        customerService.ChangeStaus(customerId,status);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success",true);
        return jsonObject;
    }



}
